var controller = require('../controller/index');
var user = controller.user;
var project = controller.project;
var api = controller.api;
module.exports = function(app){
    app.context.send = function(body,status){
        //if(!body.status)
        //    body.status = 200;
        this.response.body = body;
        if(status)
            this.status = status;
    };

    //首页
    app.get('/',controller.index);

    //登录
    app.post('/login',user.login);

    //--------------------后台管理-------------
    //获取项目总数
    app.get('/manager/getProjectsCounts',project.getProjectsCounts);
    //获取项目列表
    app.get('/manager/getProjects/:from/:counts',project.getProjects);
    //获取项目详情
    app.get('/manager/getProject/:_id',project.getProject);
    //新增项目
    app.post('/manager/addProject',project.addProject);
    //修改项目
    app.post('/manager/updateProject',project.updateProject);
    //删除项目
    app.post('/manager/deleteProject',project.deleteProject)


    //获取接口总数
    app.get('/manager/getApiCounts/:project_id',api.getApisCounts);
    //获取接口列表
    app.get('/manager/getApis/:project_id/:from/:counts',api.getApis);
    //获取接口详情
    app.get('/manager/getApi/:_id',api.getApi);
    //新增接口
    app.post('/manager/addApi',api.addApi);
    //修改接口
    app.post('/manager/updateApi',api.updateApi);
    //删除接口
    app.post('/manager/deleteApi',api.deleteApi)


    //--------------------------前台页面---------------
    //获取项目总数
    app.get('/getProjectsCounts',project.getProjectsCounts);
    //获取项目列表
    app.get('/getProjects/:from/:counts',project.getProjects);
    //获取项目详情
    app.get('/getProject/:_id',project.getProject);
    //获取接口总数
    app.get('/getApiCounts/:project_id',api.getApisCounts);
    //获取接口列表
    app.get('/getApis/:project_id/:from/:counts',api.getApis);
    //获取接口详情
    app.get('/getApi/:_id',api.getApi);


    //upload file
    app.post('manager/upload', function* () {
            console.log(this.req);
            var upload = yield require('koa-save-to-file')
                .saveToFile(this.req,this.config.rootDir + '/public/upload',{limit:5*1024});
            this.response.body = upload;
        }
    )
    //前端构造请求
    app.post('/request',function* (){
        var params  = this.request.body;
        var request = require('koa-request');
        var res = yield request(params);
        var data = {};
        data.body = res.body;
        data.headers = res.headers;
        this.response.body = data;
    })
    app.get('test',function* (){
        var res = this.response;
        var promise = new Promise();
        setTimeout(function(){
            promise._resolve();
        },5000)
    })

};